package pri.ydy.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import pri.ydy.eduService.entity.EduSubject;
import pri.ydy.eduService.entity.excel.SubjectData;
import pri.ydy.eduService.entity.subject.OneSubject;
import pri.ydy.eduService.entity.subject.TwoSubject;
import pri.ydy.eduService.listener.SubjectExcelListen;
import pri.ydy.eduService.mapper.EduSubjectMapper;
import pri.ydy.eduService.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-10-05
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            //获取文件输入流
            InputStream in=file.getInputStream();
            //调用easyExcel的读文件方法
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListen()).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //课程分类列表(树形)
    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        //单条件查询
        //1查询所有一级分类 parentid=0
        QueryWrapper<EduSubject> wrapper1=new QueryWrapper<>();
        wrapper1.eq("parent_id","0");
        //baseMapper对象是自动帮我们注入
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapper1);

        //2查询所有二级分类 parentId!=0
        QueryWrapper<EduSubject> wrapper2=new QueryWrapper<>();
        wrapper1.ne("parent_id","0");
        List<EduSubject> twoSubjects = baseMapper.selectList(wrapper2);

        //创建list集合，用于最终封装数据
        List<OneSubject> finalSubject=new ArrayList<>();

        //3封装一级分类
        //查询出来所有的一级分类list集合，得到每一个一级分类对象，获取每个一级分类对象值
        //封装到要求的list集合里面 List<OneSubject> finalSubject
        for (int i = 0; i < oneSubjects.size(); i++) {
            //得到oneSubjects的每个eduSubject对象
            EduSubject eduSubject = oneSubjects.get(i);

            //把eduSubject里面值取出来，放到OneSubject对象里面
            //多个OneSubject放到finalSubject里面
            OneSubject oneSubject =new OneSubject();
            //oneSubject.setId(eduSubject.getId());
            //oneSubject.setTitle(eduSubject.getTitle());
            BeanUtils.copyProperties(eduSubject,oneSubject);


            //在一级分类中封装二级分类
            //创建一个二级分类集合
            List<TwoSubject> twoSubjectList=new ArrayList<>();
            for (int j = 0; j < twoSubjects.size(); j++) {
                //获取当前对象
                EduSubject tSubject=twoSubjects.get(j);
                //判断pid是否为上级id
                if (tSubject.getParentId().equals(eduSubject.getId())){
                    //先new一个TwoSubject对象，然后复制
                    TwoSubject twoSubject=new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    //将对象放入二级数组
                    twoSubjectList.add(twoSubject);
                }
            }
            //将二级数组放入一级分类的子中
            oneSubject.setChildren(twoSubjectList);
            //将一级分类放入结果分类中
            finalSubject.add(oneSubject);
        }
        //4封装二级分类
        return finalSubject;
    }
}
